﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acc.Lib;
using Data.UserDb;

namespace Acc_Web
{
    public partial class Present : System.Web.UI.Page
    {
        protected Items item;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                var usrinfo = CookiesFunc.GetUserCookie();
                if(usrinfo == null)
                {
                    Response.Write("<script>alert('请先登录');history.back();</script>");
                    Response.End();
                    return;
                }
                var reqItemId = Request["i"];
                if (String.IsNullOrEmpty(reqItemId))
                {
                    Response.Write("<script>alert('参数出错');history.back();</script>");
                    Response.End();
                    return;
                }
                var itemId = 0;
                Int32.TryParse(reqItemId, out itemId);
                if (itemId == 0)
                {
                    Response.Write("<script>alert('参数出错');history.back();</script>");
                    Response.End();
                    return;
                }

                var sr = new ShopRepository();
                item = sr.GetItemDetail(itemId);
                ViewState["item"] = item.Serialize();
            }
        }

        protected void send_Click(object sender, ImageClickEventArgs e)
        {
            var un = username.Text.Trim();
            var cu = cusername.Text.Trim();
            item = ViewState["item"].ToString().Deserialize<Items>();
            if(cu.CompareTo(un) != 0)
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", String.Format("<script>alert('两次出入的帐号不一致');</script>"));
                return;
            }

            try
            {
                var usr = new UserInfo();
                usr.GetInfoByUserName(un);
                Response.Redirect(String.Format("buy.aspx?i={0}&tou={1}", item.ItemId, un));
            }
            catch
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", String.Format("<script>alert('没有该用的信息');</script>"));
                return;
            }
        }
    }
}
